#include "bimossensorpressure.h"


BimosSensorPressure::BimosSensorPressure(int ordinal) : BimosSensor(ordinal),
    slope(0), intercept(0), volumeBottle(0), volumeSludge(0), volumeSubstrat(0),
    evacuationOffset(0), prePressure(0)
{

}

double BimosSensorPressure::getEvacuationOffset(){
    return evacuationOffset;
}

double BimosSensorPressure::getPrePressure(){
    return prePressure;
}


double BimosSensorPressure::getMeanPressure(){
    return getPressure(getMeanVolt());
}


void BimosSensorPressure::setVolt(double newValue){
    BimosSensor::setVolt(newValue);
    emit pressureChanged(this);
}

double BimosSensorPressure::getPressure(double v){
    if (slope == 0 || intercept == 0){
        return v * DEFAULT_SLOPE + DEFAULT_INTERCEPT;
    } else {
        return v * slope + intercept;
    }
}
double BimosSensorPressure::getPressure(){
    return getPressure(volt) + evacuationOffset - prePressure;
}

void BimosSensorPressure::setPrePressure(double pp) {
    prePressure = pp;
}

void BimosSensorPressure::setEvacuationOffset(double absoultPressure) {
    evacuationOffset += getMeanPressure();
    evacuationOffset -= absoultPressure;
    qDebug() << "EvacuationOffset:" <<evacuationOffset;
}

double BimosSensorPressure::getVolume() {
    return volumeBottle - volumeSludge - volumeSubstrat;
}

double BimosSensorPressure::getGasYield(double steamPressure, double temperature) {
    return (getVolume() * ((getPressure() - steamPressure)* NORM_TEMPERATURE))
            / (NORM_PRESSURE * (temperature + 273.15));
}


QDataStream& operator<<(QDataStream &outStream, const BimosSensorPressure &data)
{

    outStream << *((BimosSensor*) &data);
    //outStream << (quint8)data.ordinal;

    outStream << (qreal)data.slope;
    outStream << (qreal)data.intercept;
    outStream << (qreal)data.volumeBottle;
    outStream << (qreal)data.volumeSludge;
    outStream << (qreal)data.volumeSubstrat;

    return outStream;
}

QDataStream& operator>>(QDataStream &inStream, BimosSensorPressure &data)
{

    inStream >> *((BimosSensor*) &data);
    //inStream >> data.ordinal;

    inStream >> data.slope;
    inStream >> data.intercept;
    inStream >> data.volumeBottle;
    inStream >> data.volumeSludge;
    inStream >> data.volumeSubstrat;

    return inStream;
}
